home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 342_01.zip / DIOTST01.C < prev    next >
C/C++ Source or Header  |  1993-04-03  |  7KB  |  231 lines

  1. /*-
  2.  *  ----------------------------------------------------------------------
  3.  *  File        :   DIOTST01.C
  4.  *  Creator     :   Blake Miller
  5.  *  Version     :   01.01.00        February 1991
  6.  *  Language    :   Microsoft C     Version 5.1
  7.  *  Purpose     :   Intel 8255 Compatible Digital IO Functions
  8.  *              :   Test Program #1
  9.  *  ----------------------------------------------------------------------
  10.  *  Link : DIOLIB?.LIB
  11.  *  ----------------------------------------------------------------------
  12.  *  Revision History:
  13.  *  022891 BVM  :   Change int to short.
  14.  *  091190 BVM  :   Creation
  15.  *  ----------------------------------------------------------------------
  16.  */
  17.  
  18. #define P_NAME  "DIOTST01"
  19. #define P_TITL  "DIOLIB Digital IO Function Demonstration"
  20. #define P_AUTH  "Blake Miller"
  21. #define P_VERS  "01.01.00"
  22. #define P_DATE  "February 28, 1991"
  23. #define P_PURP  "Toggle bits at the 8255"
  24.  
  25. /*  Include Files ----------------------------*/
  26.  
  27. #define     DIOTST01_C_DEFINED  1
  28. #include    <conio.h>
  29. #include    <stdio.h>
  30. #include    <stdlib.h>
  31. #include    "DIOLIB.H"
  32. #undef      DIOTST01_C_DEFINED
  33.  
  34. /*  Function Prototype -----------------------*/
  35.  
  36. void kb_dump (void);    /* keyboard flush function  */
  37. void kb_wait (void);    /* keypress wait / message  */
  38.  
  39. /*-
  40.  *  ----------------------------------------------------------------------
  41.  *  Function Definition
  42.  *  ----------------------------------------------------------------------
  43.  */
  44.  
  45. void main (void)
  46.     {
  47.     short           i;          /* counter      */
  48.     short           bitnum;     /* bit number   */
  49.     short           bstate;     /* bit state    */
  50.     char            s[32];      /* gets buffer  */
  51.     unsigned char   prtbyt;     /* port byte    */
  52.     DIODAT          dioprt;     /* port data    */
  53.  
  54.     /*  Print Program Header
  55.      */
  56.     printf ("\n" );
  57.     printf ("Program    : %s - %s.\n", P_NAME, P_TITL );
  58.     printf ("Author     : %s\n", P_AUTH );
  59.     printf ("Version    : %s  %s\n", P_VERS, P_DATE );
  60.     printf ("Purpose    : %s.\n", P_PURP );
  61.     printf ("\n" );
  62.     printf ("This program will fiddle with the bits of the 8255.\n");
  63.     printf ("\n");
  64.  
  65.     /*  Prompt user for address of the 8255.
  66.      *  Exit if an empty string is entered.
  67.      */
  68.     printf ("NOTE: 300 Hex is 768 in decimal.\n");
  69.     printf ("Enter the base address of the 8255 in decimal : ");
  70.     gets (s);
  71.     if ( *s == 0 ){
  72.         printf ("Empty string detected.  Exiting...\n");
  73.         exit (0);
  74.         }
  75.  
  76.     i = atoi(s);            /* get address of board     */
  77.     dio_init (&dioprt, i);  /* initialize port data     */
  78.  
  79.     /*  --------------------------------------------------------
  80.      *  Digital Output Demonstration
  81.      *  --------------------------------------------------------
  82.      */
  83.  
  84.     printf ("\n" );
  85.     printf ("Digital Output Demonstration.\n");
  86.     printf ("Set up output detection circuitry on ANY of the 8255\n");
  87.     printf ("output lines.  They will be toggled, flashed, etc.\n");
  88.     printf ("ALL lines will be set for output so remove any driving\n");
  89.     printf ("(INPUT) circuitry at this time, please.\n");
  90.     kb_wait();
  91.  
  92.     dio_config (&dioprt, 0, 0, 0, 0);   /* all ports output     */
  93.  
  94.     printf ("Clear all bits in digital IO...\n");
  95.     dio_dump_bytes (&dioprt);           /* all bits clear       */
  96.  
  97.     printf ("Toggle bits in digital IO...\n");
  98.  
  99.     bitnum = 0;
  100.  
  101.     while ( 1 ){
  102.  
  103.         if ( kbhit() ) break;
  104.  
  105.         dio_bitput (&dioprt, bitnum, 0);    /* clear bit (OFF)      */
  106.         bitnum++;
  107.         bitnum = (bitnum % 24);             /* bits from 0 to 23    */
  108.         dio_bitput (&dioprt, bitnum, 1);    /* set   bit (ON )      */
  109.  
  110.         for ( i = 0; i < 200; i++ ){
  111.             printf ("......\r");
  112.             }
  113.  
  114.         }
  115.  
  116.     printf ("Set all bits in digital IO...\n");
  117.     dio_put_byte (&dioprt, DIO_PORTA, 0xFF);
  118.     dio_put_byte (&dioprt, DIO_PORTB, 0xFF);
  119.     dio_put_byte (&dioprt, DIO_PORTC, 0xFF);
  120.     kb_wait();
  121.  
  122.     printf ("Clear all bits in digital IO...\n");
  123.     dio_put_byte (&dioprt, DIO_PORTA, 0);
  124.     dio_put_byte (&dioprt, DIO_PORTB, 0);
  125.     dio_put_byte (&dioprt, DIO_PORTC, 0);
  126.     kb_wait();
  127.  
  128.     printf ("Set all bits in digital IO Port A...\n");
  129.     dio_put_byte (&dioprt, DIO_PORTA, 0xFF);
  130.     kb_wait();
  131.     dio_put_byte (&dioprt, DIO_PORTA, 0);
  132.  
  133.     printf ("Set all bits in digital IO Port B...\n");
  134.     dio_put_byte (&dioprt, DIO_PORTB, 0xFF);
  135.     kb_wait();
  136.     dio_put_byte (&dioprt, DIO_PORTB, 0);
  137.  
  138.     printf ("Set all bits in digital IO Port C Low...\n");
  139.     dio_put_byte (&dioprt, DIO_PORTC, 0x0F);
  140.     kb_wait();
  141.     dio_put_byte (&dioprt, DIO_PORTC, 0);
  142.  
  143.     printf ("Set all bits in digital IO Port C High...\n");
  144.     dio_put_byte (&dioprt, DIO_PORTC, 0xF0);
  145.     kb_wait();
  146.     dio_put_byte (&dioprt, DIO_PORTC, 0x00);
  147.  
  148.  
  149.     /*  --------------------------------------------------------
  150.      *  Digital Input Demonstration
  151.      *  --------------------------------------------------------
  152.      */
  153.  
  154.     printf ("\n" );
  155.     printf ("Digital Input Demonstration.\n");
  156.     printf ("Set up some input circuitry on ANY of the 8255\n");
  157.     printf ("digital I/O lines.  They will be sensed.\n");
  158.     printf ("ALL lines will be set for input so remove any detection\n");
  159.     printf ("(OUTPUT) circuitry at this time, please.\n");
  160.     kb_wait();
  161.  
  162.     dio_config (&dioprt, 1, 1, 1, 1);   /* all ports input  */
  163.     dio_load_bytes (&dioprt);           /* all bits read    */
  164.  
  165.     printf ("Read ALL bits in digital IO...\n");
  166.     for ( bitnum = 0; bitnum <= 23; bitnum++ ){
  167.         dio_bitget (&dioprt, bitnum, &bstate);
  168.         printf ("Bit %2d is ", bitnum);
  169.         if ( bstate )   printf ("SET  \n");
  170.         else            printf ("CLEAR\n");
  171.         }
  172.     kb_wait();
  173.  
  174.     printf ("Set any new input configuration before all ports\n");
  175.     printf ("will be sensed and the settings printed out.\n");
  176.     kb_wait();
  177.  
  178.     dio_get_byte (&dioprt, DIO_PORTA, &prtbyt);
  179.     printf ("Data for Port A is %02X\n", (unsigned int) prtbyt);
  180.     dio_get_byte (&dioprt, DIO_PORTB, &prtbyt);
  181.     printf ("Data for Port B is %02X\n", (unsigned int) prtbyt);
  182.     dio_get_byte (&dioprt, DIO_PORTC, &prtbyt);
  183.     printf ("Data for Port C is %02X\n", (unsigned int) prtbyt);
  184.     kb_wait();
  185.  
  186.     printf ("That is the end of this demonstration program.\n");
  187.     kb_wait();
  188.  
  189.     exit ( 0 );
  190.     }
  191.  
  192. /*-
  193.  *  ----------------------------------------------------------------------
  194.  *  Support Function Definitions
  195.  *  ----------------------------------------------------------------------
  196.  */
  197.  
  198. /*- Flush Keyboard ----------------------------**
  199.  *  Check for keypress, and read them out of
  200.  *  the buffer until there are no more.
  201.  */
  202. void kb_dump (void)
  203.     {
  204.     while ( kbhit() ){              /* key was pressed          */
  205.         if ( (getch() == 0x00) ||   /* read out keypress        */
  206.              (getch() == 0xE0) )
  207.             getch();                /* no function key codes    */
  208.         }
  209.     }
  210.  
  211. /*- Keyboard Wait ----------------------------**
  212.  *  Flush keyboard.
  213.  *  Print blank line, then print a message to press
  214.  *  spacebar to continue, then wait for keypress, then
  215.  *  erase message and return.
  216.  */
  217. void kb_wait (void)
  218.     {
  219.     kb_dump ();
  220.     printf ("\r\n");
  221.     printf ("[ PRESS <SPACE-BAR> TO CONTINUE ]\r");
  222.     if ( getch() == 0 ) getch();
  223.     printf ("                                 \r");
  224.     }
  225.  
  226. /*-
  227.  *  ----------------------------------------------------------------------
  228.  *  END DIOTST01.C Source File
  229.  *  ----------------------------------------------------------------------
  230.  */
  231.